﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using WeSLog.model;
using WeSLog.util;
using System.Data;
using System.ServiceModel.Channels;
using System.Configuration;

namespace WeSLog
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class WeSLog : IWeSLog
    {
        public bool checkAuthenticatedIp(string ipaddress)
        {

            string whitelist = ConfigurationManager.AppSettings["allowedIp"];
            string[] result  = whitelist.Trim().Split(';');
            foreach (string sub in result)
            {
                if (ipaddress == sub)
                {
                    return true;
                }
                else
                    return false;
            }
            return true;
        }
        //
        public WeSLogEnqueueResult logToDatabase(Log log)
        {
            //lay ip adress
            OperationContext context = OperationContext.Current;
            MessageProperties messageProperties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpointProperty =
              messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            //
            if (!checkAuthenticatedIp(endpointProperty.Address))
            {
                return new WeSLogEnqueueResult
                {
                    weSLogResultStatus = new WeSLogResultStatus
                    {
                        statusCode = WeSLogResultStatus.STATUS_NOT_ALLOW_IP_CODE,
                        
                        statusText = string.Format(WeSLogResultStatus.STATUS_NOT_ALLOW_IP_TEXT,endpointProperty.Address)
                    }
                };
            }
            else
            {
                if (log.logResource != "" && log.logResource != null)
                {
                    try
                    {
                        object[,] parameter = { 
                                        {"@logContent",log.logContent},
                                        {"@logResource",log.logResource},
                                        {"@enqueueTime",DateTime.Now.ToString("yyyyMMddHHmmss")},               
                                      };

                        DB.exec("insert into [Log] (LogContent, LogResource,EnqueueTime) values (@logContent, @logResource,@enqueueTime);", parameter);
                        return new WeSLogEnqueueResult
                        {
                            weSLogResultStatus = new WeSLogResultStatus
                            {
                                statusCode = WeSLogResultStatus.STATUS_VALID_TOKEN_CODE,
                                statusText = WeSLogResultStatus.STATUS_VALID_TOKEN_TEXT
                            }
                        };
                    }
                    catch (Exception ex)
                    {
                        return new WeSLogEnqueueResult
                        {
                            weSLogResultStatus = new WeSLogResultStatus
                            {
                                statusCode = WeSLogResultStatus.STATUS_INVALID_LOG_CODE,
                                statusText = WeSLogResultStatus.STATUS_INVALID_LOG_TEXT
                            }
                        };
                    }
                }
                else
                {
                    return new WeSLogEnqueueResult
                    {
                        weSLogResultStatus = new WeSLogResultStatus
                        {
                            statusCode = WeSLogResultStatus.STATUS_INVALID_LOG_CODE,
                            statusText = WeSLogResultStatus.STATUS_INVALID_LOG_TEXT
                        }
                    };
                }
            }
        }
    }
}
